/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.motoo.codec.textline;

import java.nio.charset.Charset;

import com.google.code.motoo.codec.ProtocolCodecFactory;
import com.google.code.motoo.codec.ProtocolDecoder;
import com.google.code.motoo.codec.ProtocolEncoder;

/**
 * ProtocolCodec的工厂类，每次返回一个新的Codec的实例
 * @author linfengqi
 * @version 1.0  May 13, 2009
 */
public class TextLineCodecFactory implements ProtocolCodecFactory {

    private final Charset charset;
    private final LineDelimiter encodingDelimiter;
    private final LineDelimiter decodingDelimiter;

    public TextLineCodecFactory() {
        this(Charset.defaultCharset());
    }
    
    public TextLineCodecFactory(String charset) {
        this(Charset.forName(charset));
    }

    public TextLineCodecFactory(Charset charset) {
    	this(Charset.defaultCharset(), LineDelimiter.WINDOWS, LineDelimiter.WINDOWS);
    }

    public TextLineCodecFactory(Charset charset,
            String encodingDelimiter, String decodingDelimiter) {
    	this(Charset.defaultCharset(), new LineDelimiter(encodingDelimiter), new LineDelimiter(decodingDelimiter));
    }

    public TextLineCodecFactory(Charset charset, LineDelimiter encodingDelimiter, LineDelimiter decodingDelimiter) {
       this.charset = charset;
       this.encodingDelimiter = encodingDelimiter;
       this.decodingDelimiter = decodingDelimiter;
    }

    public ProtocolEncoder getEncoder() {
        return new TextLineEncoder(charset, encodingDelimiter);
    }

    public ProtocolDecoder getDecoder() {
        return new TextLineDecoder(charset, decodingDelimiter);
    }

}
